<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/controller/ctlAbstract.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busItem.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busResource.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busComment.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busUser.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busProvider.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busCategory.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busQuestion.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/item.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/favourite.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/resource.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/comment.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/rating.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/provider.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/utility/stringUtil.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/utility/simpleImage.php';

class ItemController extends AbstractController {
	private $_itemBus;
	private $_resourceBus;
	private $_commentBus;
	private $_userBus;
	private $_categoryBus;
	private $_questionBus;
	private $_providerBus;

	public function main(array $getVars) {
		global $ubi;

		$this->_itemBus = new ItemBus();
		$this->_resourceBus = new ResourceBus();
		$this->_commentBus = new CommentBus();
		$this->_userBus = new UserBus();
		$this->_categoryBus = new CategoryBus();
		$this->_questionBus = new QuestionBus();
		$this->_providerBus = new ProviderBus();

		$userID = $_SESSION['userid'];

		if ($getVars['action'] == 'favourite') {
			if ($userID != null) {
				echo $this->_itemBus->favourite($_POST['id'], $userID);
			}
		} else if ($getVars['action'] == 'unfavourite') {
			if ($userID != null) {
				echo $this->_itemBus->removeFavourite($_POST['id'], $userID);
			}
		} else if ($getVars['action'] == 'plus') {
			if ($userID != null) {
				echo $this->_itemBus->plus($_POST['id'], $userID);
			}
		} else if ($getVars['action'] == 'minus') {
			if ($userID != null) {
				echo $this->_itemBus->minus($_POST['id'], $userID);
			}
		} else if ($getVars['action'] == 'uploadImage') {
			$itemID = $getVars['id'];
			$fileName = $_FILES['imgItem']['name'];
			if (file_exists($ubi['SERVER']['SERVER_ROOT'] . '/' . $ubi['SERVER']['RESOURCE_PATH'] . '/' . $fileName)) {
				$fileName = StringUtil::changeFileName($fileName);
			}
			
			//create resource model object
			$res = new ResourceModel();
			$res->setResourceName($fileName);
			$res->setShortDescription('item');
			
			$id = $this->_resourceBus->addResource($res);
				
			if ($id != 0) {
				$item = $this->_itemBus->getItem($itemID);

				$numRow = $this->_itemBus->addResource($item, $id);

				if ($numRow != 0) {
					$fileSavedResourceName = $ubi['SERVER']['SERVER_ROOT'] . '/' . $ubi['SERVER']['RESOURCE_PATH'] . '/' . $fileName;
					$fileSavedThumbnailName = $ubi['SERVER']['SERVER_ROOT'] . '/' . $ubi['SERVER']['THUMBNAIL_PATH'] . '/' . $fileName;
					if (move_uploaded_file($_FILES['imgItem']['tmp_name'], $fileSavedResourceName)) {
						copy($fileSavedResourceName, $fileSavedThumbnailName);
						
						$image = new SimpleImage();
						$image->load($fileSavedThumbnailName);
						$image->resize($ubi['SITE']['WIDTH_THUMBNAIL'], $ubi['SITE']['HEIGHT_THUMBNAIL']);
						$image->save($fileSavedThumbnailName);

						$image->load($fileSavedResourceName);
						$image->resize($ubi['SITE']['WIDTH_RESOURCE'], $ubi['SITE']['HEIGHT_RESOURCE']);
						$image->save($fileSavedResourceName);
						
						echo '1';
						return;
					}
				}
			}
			echo '0';
		} else if ($getVars['action'] == 'loadImage') {
			$itemID = $getVars['id'];
			$itemModel = $this->_itemBus->getItem($itemID);
			$arrResID = split(' ', trim($itemModel->getResourceList()));
				
			foreach ($arrResID as $resID) {
				$arrRes[] = $this->_resourceBus->getResource($resID);
			}
				
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewImage.php';
		} else if ($getVars['action'] == 'plusItem') {
			if ($userID != null) {
				$itemID = $getVars['id'];
				echo $this->_itemBus->plusItem($itemID, $userID);
			}
		} else if ($getVars['action'] == 'minusItem') {
			if ($userID) {
				$itemID = $getVars['id'];
				echo $this->_itemBus->minusItem($itemID, $userID);
			}
		} else if ($getVars['action'] == 'comment') {
			if ($userID != null) {
				$commentModel = new CommentModel();
				$commentModel->setInteractType($ubi['ITEM']['VALUE']);
				$commentModel->setMessage($_POST['comment']);
				$commentModel->setUserID($userID);
				$commentModel->setIndicteeID($_POST['id']);

				echo $this->_commentBus->addComment($commentModel);
			}
		} else if ($getVars['action'] == 'loadshortinfo') {
			$id = $getVars['id'];

			$itemModel = $this->_itemBus->getItem($id);

			$userModel = $this->_userBus->getUser($itemModel->getUserID());
				
			$providerModel = $this->_providerBus->getProvider($itemModel->getProviderID());
				
			$arrCatID = split(' ', trim($itemModel->getCategoryList()));
			foreach ($arrCatID as $catID) {
				$catTemp = $this->_categoryBus->getCategoryItemFromID($catID);
				if ($catTemp) {
					$arrCat[] = $catTemp;
				}
			}
				
			$this->_questionBus->getQuestionAnswerListFromItem($id, $arrQuestion, $arrAnswer);
				
			$userID = $_SESSION['userid'];

			if ($userID != null) {
				if ($this->_itemBus->isFavourite($id, $userID)) {
					$isFavourite = true;
				}
				if ($this->_itemBus->isMinus($id, $userID)) {
					$isMinus = true;
				}
				if ($this->_itemBus->isPlus($id, $userID)) {
					$isPlus = true;
				}
			}

			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewShortInfoItem.php';
		} else if ($getVars['action'] == 'edit') {
			$userID = $_SESSION['userid'];
			if ($userID) {
				$id = $getVars['id'];
				$itemModel = $this->_itemBus->getItem($id);
				if ($userID == $itemModel->getUserID()) {
					$arrCatID = split(' ', trim($itemModel->getCategoryList()));
					$arrCat = array();
					$arrCatParent = array();
										
					foreach ($arrCatID as $catID) {
						$catTemp = $this->_categoryBus->getCategoryItemFromID($catID);
						if ($catTemp) {
							$arrCat[] = $catTemp;
							$arrCatParent[] = $this->_categoryBus->getCategoryItemFromID($catTemp->getParentID());
						}
					}
					$providerModel = $this->_providerBus->getProvider($itemModel->getProviderID()); 
					
					$arrQuestion = array();
					$arrAnswer = array();
					$this->_questionBus->getQuestionAnswerListFromItem($id, $arrQuestion, $arrAnswer);
						
					$arrCatItem1 = $this->_categoryBus->getCategoryItemList(0);
					$catItem1 = $arrCatItem1[0];
					$arrCatItem2 = $this->_categoryBus->getCategoryItemList($catItem1->getCategoryID());

					require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewEditItem.php';
				}
			}
		} else if ($getVars['action'] == 'update') {
			$userID = $_SESSION['userid'];
			if ($userID) {
				$itemModel = $this->_itemBus->getItem($_POST['itemID']);
				if ($itemModel->getUserID() == $userID) {
					$itemModel->setItem($_POST['txtItemName']);
					$itemModel->setAnswerList($_POST['answerList']);
					$itemModel->setBeginPrice($_POST['txtPriceFrom']);
					$itemModel->setEndPrice($_POST['txtPriceTo']);
					$itemModel->setUnit($_POST['selUnit']);
					$itemModel->setItemID($_POST['itemID']);
					$itemModel->setCategoryList($_POST['categoryItemList']);
					$itemModel->setDescription($_POST['txtItemDes']);
					
					if ($this->_itemBus->updateItem($itemModel) == 1) {
						$url = 'index.php?view&type=item&id=' . $itemModel->getItemID();							
						header( 'Location:' . $url);
					} else {
						$url = 'index.php?item&action=edit&id=' . $itemModel->getItemID();							
						header( 'Location:' . $url);
					}
				}
			}
		} else if ($getVars['action'] == 'loadsocialinfo') {
			$id = $getVars['id'];
			$itemModel = $this->_itemBus->getItem($id);
			$nComments = $this->_itemBus->getNumberOfComment($id);
			$nFav = $this->_itemBus->getNumberOfFavourites($id);
			
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewItemSocialInfo.php';
		}  
	}
}
?>